<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('账户名称')" prop="accountName">
            <el-input v-model="form.accountName" :placeholder="$t('账户名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券编号')" prop="bondCode">
            <el-input v-model="form.bondCode" :placeholder="$t('债券编号')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券名称')" prop="bondName">
            <el-input v-model="form.bondName" :placeholder="$t('债券名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持仓数量')" prop="numberPositions">
            <el-input v-model="form.numberPositions" :placeholder="$t('持仓数量')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券价格')" prop="bondPrice">
            <Money v-model="form.bondPrice" :placeholder="$t('债券价格')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券久期')" prop="bondDuration">
            <el-input v-model="form.bondDuration" :placeholder="$t('债券久期')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券收益率')" prop="bondYield">
            <el-input v-model="form.bondYield" :placeholder="$t('债券收益率')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持仓市值')" prop="marketPositions">
            <el-input v-model="form.marketPositions" :placeholder="$t('持仓市值')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券评级')" prop="bondRating">
            <el-input v-model="form.bondRating" :placeholder="$t('债券评级')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('主体评级')" prop="subjectRating">
            <el-input v-model="form.subjectRating" :placeholder="$t('主体评级')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持仓成本')" prop="holdingCost">
            <Money v-model="form.holdingCost" :placeholder="$t('持仓成本')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('已质押份额')" prop="pledgedShare">
            <el-input v-model="form.pledgedShare" :placeholder="$t('已质押份额')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('可质押份额')" prop="pledgeableShare">
            <el-input v-model="form.pledgeableShare" :placeholder="$t('可质押份额')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:positionInformation" />
    </div>
  </div>
</template>

<script>
import { saveBondPositionDetails, getBondPositionDetails } from '@/api/bond/positionInformation/bondPositionDetails.js'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        accountName: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        bondCode: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        bondName: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getBondPositionDetails(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        accountName: undefined,
        bondCode: undefined,
        bondName: undefined,
        numberPositions: undefined,
        bondPrice: undefined,
        bondDuration: undefined,
        bondYield: undefined,
        marketPositions: undefined,
        bondRating: undefined,
        subjectRating: undefined,
        holdingCost: undefined,
        pledgedShare: undefined,
        pledgeableShare: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveBondPositionDetails(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
